package repository

import (
	"slapp3/backend/application/controller"
	"slapp3/backend/application/domain"
)

func NewConnectRepository(data *Data) controller.ConnectRepository {
	// Return an implementation of ConnectRepository
	return &connectRepoImpl{data: data}
}

type connectRepoImpl struct {
	data *Data
}

func (r *connectRepoImpl) GetConnection(id uint) (domain.Profile, error) {
	var profile domain.Profile
	if err := r.data.db.First(&profile, id).Error; err != nil {
		return domain.Profile{}, err
	}
	return profile, nil
}
func (r *connectRepoImpl) CreateConnection(conn domain.Profile) error {
	if err := r.data.db.Create(&conn).Error; err != nil {
		return err
	}
	return nil
}

func (r *connectRepoImpl) UpdateConnection(conn domain.Profile) error {
	if err := r.data.db.Save(&conn).Error; err != nil {
		return err
	}
	return nil
}

func (r *connectRepoImpl) DeleteConnection(id uint) error {

	if err := r.data.db.Delete(&domain.Profile{}, id).Error; err != nil {
		return err
	}
	return nil
}

func (r *connectRepoImpl) ListConnections(page, pageSize int) ([]domain.Profile, int64, error) {
	var profiles []domain.Profile
	var total int64
	if err := r.data.db.Model(&domain.Profile{}).Count(&total).Error; err != nil {
		return nil, 0, err
	}
	if err := r.data.db.Model(&domain.Profile{}).Offset((page - 1) * pageSize).Limit(pageSize).Find(&profiles).Error; err != nil {
		return nil, 0, err
	}
	return profiles, total, nil
}
